//
//  DeliveryRecordController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/5/19.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "DeliveryRecordController.h"
#import "DeliveryRecordCell.h"
#import "DeliveryRecordDetailController.h"// 发货单对方已付款详情
#import "DeliveryRecordModel.h"
#import "DeliveryRecordNotPayDetailController.h"// 发货单对方未付款详情
@interface DeliveryRecordController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation DeliveryRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestListData];
}
- (void)setupUI{
    self.title = @"发货记录";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    
    self.page = 1;
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    self.tableView.rowHeight = 220;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestListData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)requestListData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNumber"] = @1;
    param[@"factoryUserId"] = self.factoryUserId;
    [NetWorkTool POST:getUserSendOrderList param:param success:^(id dic) {
        self.dataArray = [DeliveryRecordModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        self.page = 2;
        self.dataArray = [self.dataArray mutableCopy];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
- (void)loadMoreData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNumber"] = @(self.page);
    param[@"factoryUserId"] = self.factoryUserId;
    [NetWorkTool POST:getUserSendOrderList param:param success:^(id dic) {
        NSMutableArray *modelArray = [DeliveryRecordModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        if (modelArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        self.page++;
        [self.dataArray addObjectsFromArray:modelArray];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } other:^(id dic) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeliveryRecordModel *model = self.dataArray[indexPath.row];
    DeliveryRecordCell *cell = [DeliveryRecordCell cellWithTableView:tableView];
    cell.model = model;
    [cell.checkDetailButton addTarget:self action:@selector(checkDetailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.checkDetailButton.tag = indexPath.row;
    
    [cell.checkDetailButton2 addTarget:self action:@selector(checkDetailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.checkDetailButton2.tag = indexPath.row;
    
    [cell.payedButton addTarget:self action:@selector(payedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.payedButton.tag = indexPath.row;

    return cell;
    
}
// 查看详情
- (void)checkDetailButtonClick:(UIButton *)button{
    DeliveryRecordModel *model = self.dataArray[button.tag];
    if ([model.is_pay isEqualToString:@"0"]) {// 对方未付款
        DeliveryRecordNotPayDetailController * vc = [[DeliveryRecordNotPayDetailController alloc] init];
        vc.id = model.id;
        vc.confirmCustomerPayedClick = ^{
            model.is_pay = @"1";
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    } else {// 对方已付款
        DeliveryRecordDetailController * vc = [[DeliveryRecordDetailController alloc] init];
        vc.id = model.id;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
// 对方已付款
- (void)payedButtonClick:(UIButton *)button{
    DeliveryRecordModel *model = self.dataArray[button.tag];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = model.id;
    [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    [NetWorkTool POST:paySendOrder param:param success:^(id dic) {
        model.is_pay = @"1";
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
@end
